package com.mqb;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mqb
 * @date 2021/4/2 12:49
 */
@RestController
public class HelloController {
    @GetMapping("/details")
    public Map<String,Object> details(HttpServletRequest request){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();

        Map<String,Object> res = new HashMap<>(3);
        res.put("details",details);
        res.put("requestSessionId",request.getRequestedSessionId());
        res.put("session",request.getSession(false) == null ?null:request.getSession().getId());
        return res;
    }
}
